package zmee.bid.formplugin;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import org.apache.commons.collections.CollectionUtils;
import zmee.bid.oa.OAutil;
import zmee.util.BosUtil;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.Map;

/**
 * @author lijiawang
 * @Date 2022/12/16 10:04
 * @Description:定标单生成附件
 */
public class SrcDecisionFormPlugin extends AbstractFormPlugin implements BeforeF7SelectListener {
    private static Log log = LogFactory.getLog(SrcDecisionFormPlugin.class);

    @Override
    public void registerListener(EventObject e) {
        BasedataEdit base = this.getControl("oaorg");
        base.addBeforeF7SelectListener(this);
    }

    @Override
    public void afterBindData(EventObject e) {
        Object oasqr = this.getModel().getValue("oasqr");
        if (oasqr == null) {
            DynamicObject user = BusinessDataServiceHelper.loadSingle(UserServiceHelper.getCurrentUserId(), "bos_user");
            this.getModel().setValue("oasqr", user);
            DynamicObjectCollection entryentity = user.getDynamicObjectCollection("entryentity");
            DynamicObject userOrg = OAutil.getUserOrg(entryentity);
            this.getModel().setValue("oaorg", userOrg.getDynamicObject("dpt"));
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        FormOperate operate = (FormOperate) args.getSource();
        String operateKey = operate.getOperateKey();
        if ("submit".equals(operateKey)) {
            DynamicObject sourceclass = (DynamicObject) this.getModel().getValue("sourcetype");
            String byNumber = "";
            switch (sourceclass.getString("number")) {
                //招标
                case "C010405":
                    byNumber = "gkxj001";
                    break;
                //竞争性谈判
                case "C010406":
                    byNumber = "tpxj001";
                    break;
                //询价
                case "C010418":
                    byNumber = "xjxj001";
                    break;
                //竞争性磋商
                case "C010415":
                    byNumber = "csxj001";
                    break;
                default:
                    break;
            }
            boolean bool = true;
            AttachmentPanel attachmentpanel = this.getView().getControl("zmee_contractaccessory");
            if (null != attachmentpanel) {
                List<Map<String, Object>> attachmentData = attachmentpanel.getAttachmentData();
                if (CollectionUtils.isNotEmpty(attachmentData)) {
                    for (Map<String, Object> row : attachmentData) {
                        String name = row.get("name").toString();
                        log.info("attachment name：" + name);
                        if (name.contains("打印模板")) {
                            bool = false;
                        }
                    }
                }
            }
            if (bool) {
                BosUtil.createAtt(byNumber, this.getModel(), this.getView(), "attachmentpanel", "src_decision");
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        Object newValue = e.getChangeSet()[0].getNewValue();
        if ("oasqr".equals(name)) {
            if (newValue != null) {
                DynamicObject sqr = (DynamicObject) newValue;
                DynamicObject user = BusinessDataServiceHelper.loadSingle(sqr.getPkValue(), sqr.getDynamicObjectType().getName());
                DynamicObjectCollection entryentity = user.getDynamicObjectCollection("entryentity");
                DynamicObject userOrg = OAutil.getUserOrg(entryentity);
                this.getModel().setValue("oaorg", userOrg.getDynamicObject("dpt"));
            }
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent event) {
        String name = event.getProperty().getName();
        Object oasqr = this.getModel().getValue("oasqr");
        if (oasqr == null) {
            event.setCancel(true);
            this.getView().showTipNotification(ResManager.loadKDString("请先选择申请人。", "SrcDecisionFormPlugin", "pur-src-formplugin", new Object[0]));
            return;
        }
        DynamicObject sqr = (DynamicObject) oasqr;
        DynamicObject user = BusinessDataServiceHelper.loadSingle(sqr.getPkValue(), "bos_user");
        DynamicObjectCollection entryentity = user.getDynamicObjectCollection("entryentity");
        List<QFilter> list = new ArrayList<>();
        String[] str = new String[entryentity.size()];
        for (int i = 0; i < entryentity.size(); i++) {
            str[i] = entryentity.get(i).getString("dpt.id");
        }
        QFilter qFilter = new QFilter("id", QCP.in, str);
        list.add(qFilter);
        event.setCustomQFilters(list);


    }
}
